<?php
declare(strict_types=1);

namespace App\Service;

use App\Helper\Log;
use App\Helper\Util;
use Hyperf\DbConnection\Db;

/**
 * 订阅服务
 */
class SubService 
{
    /**
     * [__construct description] 构造函数
     */
    public function __construct()
    {}
    /**
     * 保存订阅
     * [personSave description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function subSave($params)
    {
        $exists_id = Db::table('user_sub')
            ->where(['user_id'=>$params['user_id'],'std_id'=>$params['grabdate_id'],'status'=>1,'is_del'=>0])
            ->value("id");
        if(!empty($exists_id)){
            $save_data = [
                'updated_at'=>time()
            ];
            if(!empty($params['sub_time'])){
                $save_data['remind_time'] = strtotime($params['sub_time']);
            }
            Db::table('user_sub')
                ->where(['id'=>$exists_id])
                ->update($save_data);
        }else{
            $save_data = [
                'user_id'=>$params['user_id'],
                'std_id'=>$params['grabdate_id'],
                'remind_time'=>strtotime($params['sub_time']),
                'created_at'=>time()
            ];
            Db::table('user_sub')
                ->insert($save_data);
        }
        return true;
    }
    /**
     * 取消订阅
     * [unSubSave description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function unSubSave($params)
    {
        $exists_id = Db::table('user_sub')
            ->where(['user_id'=>$params['user_id'],'std_id'=>$params['grabdate_id'],'status'=>1,'is_del'=>0])
            ->value("id");
        if(!empty($exists_id)){
            $save_data = [
                'status'=>0,
                'is_del'=>1,
                'updated_at'=>time()
            ];
            Db::table('user_sub')
                ->where(['id'=>$exists_id])
                ->update($save_data);
        }
        return true;
    }
}
